package com.atguigu.msm.service.impl;


import com.atguigu.msm.entity.UcenterMember;
import com.atguigu.msm.mapper.UcenterMemberMapper;
import com.atguigu.msm.service.MsmCacheService;
import com.atguigu.msm.service.UcenterMemberService;
import com.atguigu.msm.vo.RegisterVo;
import com.atguigu.serviceBase.exception.GuliException;
import com.atguigu.utils.JwtUtils;
import com.atguigu.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Summer-day
 * @since 2020-07-30
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    private static final Logger log = LoggerFactory.getLogger(UcenterMemberServiceImpl.class);


    @Autowired
    private MsmCacheService msmCacheService;

    @Override
    public String login(UcenterMember member) {

        String mobile = member.getMobile();

        String password = member.getPassword();

        //手机号密码校验
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException("登录失败");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", member.getMobile());
        //判断手机号是否正确
        UcenterMember memberFromDataBase = baseMapper.selectOne(wrapper);
        if (memberFromDataBase == null) {
            throw new GuliException("手机号错误!");
        }

        String encrypt = MD5.encrypt(password);
        log.info("输入密码的MD5 ==> {}", encrypt);
        if (!encrypt.equals(memberFromDataBase.getPassword())) {
            throw new GuliException("密码错误!");
        }

        //判断用户是否被禁用
        if (memberFromDataBase.getIsDisabled()) {
            throw new GuliException("登录失败");
        }

        //生成token字符串
        return JwtUtils.getJwtToken(memberFromDataBase.getId(), memberFromDataBase.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {

        String code = registerVo.getCode();
        String phone = registerVo.getMobile();
        String nickName = registerVo.getNickName();
        String password = registerVo.getPassword();

        //非空检验
        if (StringUtils.isEmpty(phone)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(nickName)
                || StringUtils.isEmpty(code)) {
            throw new GuliException("注册失败");
        }

        if (!verifyAuthCode(phone, code)) {
            throw new GuliException("验证码错误!");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        if(baseMapper.selectCount(wrapper)>0){
            throw new GuliException("注册失败!");
        }

        //数据添加到数据库
        UcenterMember member = new UcenterMember();
        member.setMobile(phone);
        member.setNickname(nickName);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);

        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Integer countRegisterNum(String date) {

        return baseMapper.countRegisterNum(date);

    }

    /**
     * 验证验证码是否正确
     *
     * @param phone 电话
     * @param code  验证码
     */
    private boolean verifyAuthCode(String phone, String code) {
        if (StringUtils.isEmpty(code)) {
            return false;
        }
        String authCode = msmCacheService.getAuthCode(phone);
        return code.equals(authCode);
    }

}
